/**
 * Common Test Utility Functions for vPager.
 * To be used with Jasmine Test "vPager-Spec.js"
 */

var VPC = {};

VPC.captureException = function(func, arg) {
	var ex = null;
	try {
		func(arg);
	}
	catch (e) {
		ex = e;
	}
	return ex;
};

VPC.testParentPageAdd = function(page_data) {
	return VPC.captureException(new VPAGER.ServerProxy().submitNewPage, 
							    new VPAGER.ParentPage(page_data));
};

VPC.testBadParentPageCode = function(page_code) {
	var exception = VPC.testParentPageAdd({"Code":page_code});
	expect(exception).not.toBeNull();
	expect(exception.errorCode).toEqual(10409);
};

VPC.testDisplayViewState = function(view_state) {
	var params = new VPAGER.DisplayParams();
	params.setViewState(view_state);
	return VPC.captureException(new VPAGER.ServerProxy().setDisplayParams, params);
};

VPC.createProductionLogger = function() {
     // Setup log4javascript logger and popup appender.
    var log4js = log4javascript.getLogger();
    var popUpAppender = new log4javascript.PopUpAppender();
    var popUpLayout = new log4javascript.PatternLayout("%d{HH:mm:ss} %-5p - %m%n");
    popUpAppender.setLayout(popUpLayout);
    popUpAppender.setThreshold(log4javascript.Level.TRACE);
    log4js.addAppender(popUpAppender);
    log4js.setLevel(log4javascript.Level.TRACE);
    
    // Create an anonymous logger object to redirect log message to TRACE.
    var logger = {
        ID: "L4JS", 
        log: function(msg){
            log4js.debug(msg);
        }
    };
    
    return logger;
};

VPC.SimpleListener = function() {
    this._called = 0;
    this._type = undefined;
    this._event = undefined;
    this.onVPagerEvent = function(type, event) {
        this._called++;
        this._type = type;
        this._event = event;   
    };
};

